package com.start.cms.api;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.cms.entity.StartArticleBrowseRecordsEntity;
import com.start.cms.entity.StartCmsCategoryEntity;
import com.start.cms.entity.VStartCmsArticleDetailEntity;
import com.start.cms.model.StartSpecialContentModel;
import com.start.cms.service.StartArticleBrowseRecordsService;
import com.start.cms.service.StartCmsArticleService;
import com.start.cms.service.StartCmsCategoryService;
import com.start.cms.service.StartCmsSpecialService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.details.BaseUserDetails;
import com.start.common.tools.IPTools;
import com.start.common.tools.SpringContextTools;
import com.start.common.utils.TreeModelUtils;
import com.start.security.StartTokenTools;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "cms/api")
@RequestMapping("cms/api")
@RestController
public class StartCmsController {
    @Autowired
    private StartCmsArticleService articleService;

    @Autowired
    private StartCmsCategoryService categoryService;

    @Autowired
    private StartCmsSpecialService cmsSpecialService;
    @Autowired
    private StartArticleBrowseRecordsService browseRecordsService;

    @GetMapping("/articlePage")
    public Result<?> articlePage(Page<VStartCmsArticleDetailEntity> page, VStartCmsArticleDetailEntity entity) {
        LambdaQueryWrapper<VStartCmsArticleDetailEntity> queryWrapper = DataQueryConstructor.createEntity(VStartCmsArticleDetailEntity.class).lambda();
        if (StringUtils.isEmpty(entity.getCategoryId())) {
            StartCmsCategoryEntity cmsCategory = categoryService.getById(entity.getCategoryId());
            if (cmsCategory != null) {
                String levelIds = cmsCategory.getParentIds() + cmsCategory.getId() + "," ;
                queryWrapper.likeRight(VStartCmsArticleDetailEntity::getCategoryParentIds, levelIds);
            }
        }
        return Result.ok(articleService.pageArticleDetail(page, queryWrapper));
    }

    @GetMapping("/articleDetailById/{articleId}")
    public Result<?> articleDetailById(@PathVariable("articleId") String articleId) {
        BaseUserDetails currentUser = StartTokenTools.getCurrentUser(false);
        VStartCmsArticleDetailEntity vStartCmsArticleDetailEntity = articleService.articleDetailById(articleId);
        if (vStartCmsArticleDetailEntity == null) return Result.ok();
        vStartCmsArticleDetailEntity.setArticlePv((vStartCmsArticleDetailEntity.getArticlePv() == null ? 0 : vStartCmsArticleDetailEntity.getArticlePv()) + 1);
        StartArticleBrowseRecordsEntity record = null;
        if (currentUser == null) {
            record = new StartArticleBrowseRecordsEntity();
            record.setUserId("-1");
        } else {
            record = browseRecordsService.getRecordByArticleIdAndUserId(currentUser.getUserId(), articleId);
            if (record == null) {
                record = new StartArticleBrowseRecordsEntity();
            }
            record.setUserId(currentUser.getUserId());
            vStartCmsArticleDetailEntity.setArticleUv((vStartCmsArticleDetailEntity.getArticleUv() == null ? 0 : vStartCmsArticleDetailEntity.getArticleUv()) + 1);
        }
        record.setUserIp(IPTools.getIpAddr(SpringContextTools.getHttpServletRequest()));
        record.setArticleId(articleId);
        record.setLastBrowseTime(LocalDateTime.now());
        articleService.updateArticlePvUv(articleId, vStartCmsArticleDetailEntity.getArticlePv(), vStartCmsArticleDetailEntity.getArticleUv());
        browseRecordsService.saveOrUpdate(record);
        return Result.ok(vStartCmsArticleDetailEntity);
    }


    @GetMapping("/categoryList")
    public Result<?> loadCategory(String parentIds, boolean isTree) {
        final List<StartCmsCategoryEntity> list = categoryService.loadCategory(parentIds);
        if (list.isEmpty()) return Result.ok(list);
        if (isTree) {
            return Result.ok(TreeModelUtils.createTree(list));
        }
        return Result.ok(list);
    }

    @GetMapping("/categoryById/{categoryId}")
    public Result<?> categoryById(@PathVariable("categoryId") String categoryId) {
        return Result.ok(categoryService.getById(categoryId));
    }

    @GetMapping("/categoryPath/{categoryId}")
    public Result<?> categoryPath(@PathVariable("categoryId") String categoryId) {
        return Result.ok(categoryService.categoryPath(categoryId));
    }


    @GetMapping("/getSpecialContentBySpecialCode/{specialCode}")
    public Result<?> getSpecialContentBySpecialCode(@PathVariable("specialCode") String specialCode) {
        StartSpecialContentModel startSpecialContentModel = cmsSpecialService.getSpecialContentBySpecialCode(specialCode);
        return Result.ok(startSpecialContentModel);
    }

    @GetMapping("/getDiyModelConfig/{modelCode}")
    public Result<?> getDiyModelConfig(@PathVariable("modelCode") String modelCode) {

        return Result.ok();
    }

    @GetMapping("/saveDiyModelData/{modelCode}")
    public Result<?> saveDiyModelData(@PathVariable("modelCode") String modelCode, @RequestBody JSONObject data) {

        return Result.ok();
    }

    @GetMapping("/getDiyModelDataById/{modelCode}")
    public Result<?> getDiyModelDataById(@PathVariable("modelCode") String modelCode, String id) {
        return Result.ok();
    }

    @GetMapping("/listDiyModelDataByModeCode/{modelCode}")
    public Result<?> listDiyModelDataByModeCode(@PathVariable("modelCode") String modelCode) {
        DataQueryConstructor.createStartQueryWrapper();
        return Result.ok();
    }

    @GetMapping("/pageDiyModelDataByModelCode/{modelCode}")
    public Result<?> pageDiyModelDataByModelCode(@PathVariable("modelCode") String modelCode) {
        return Result.ok();
    }
}
